import { useStore,mapState,mapMutations,mapGetters,mapActions  } from 'vuex'
import { computed } from 'vue';
const hooks = (mapperFn, mapper:Array<string>, module:string) => {
    const store = useStore();
    const resultFn = {};
    let mapData = {};
    if (module) {
      mapData = mapperFn(module, mapper);
    } else {
      mapData = mapperFn(mapper);
    }
    Object.keys(mapData).forEach( item => {
      const fn = mapData[item].bind({'$store': store});
      resultFn[item] = fn;
    });
    return resultFn
};
const useDataHooks = (mapperFn, mapper:Array<string>, module:string) => {
    let hooksData = hooks( mapperFn, mapper, module);
    const resultFn = {};
    const result = {};
    Object.keys(hooksData).forEach( item => {
      resultFn[item] = computed(hooksData[item]);
      result[item] = resultFn[item].value
    });
    return result
}
export const useStates = (mapper:Array<string>, module:string) => {
    return useDataHooks(mapState, mapper, module)
};
export const useGetters = (mapper:Array<string>, module:string) => {
    return useDataHooks(mapGetters, mapper, module)
};
export const useMutations = (mapper:Array<string>, module:string) => {
    return hooks( mapMutations , mapper, module);
};
export const useActions = (mapper:Array<string>, module:string) => {
    return hooks( mapActions , mapper, module);
};